package sg.edu.nus.iss.ooad.jass.ui.widget;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 *
 * Template Dialog Window for JASS.
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0
 *
 * Development Environment : Netbeans IDE v6.7
 * Name of the File        : JASSDialog.java
 * Creation / Modification History
 *         Isak Rabin        03-Oct-2009        Created
 *
 */
public class JASSDialogWindow extends Dialog implements ActionListener {

        private static final long serialVersionUID = 6381840681965947110L;
        private boolean stillWaiting = true;
        //Form components
        public Label labelMessage = null;
        public Button buttonOK = null;
        public Panel formPanel = null;

        /**
         *
         * @param parent
         * @param title
         * @param dimension
         * @param modal
         * @param msg
         */
        public JASSDialogWindow(Frame parent, String title, Dimension dimension, boolean modal, String msg) {
                super(parent, title, modal);

                labelMessage = new Label();
                labelMessage.setText(msg);
                labelMessage.setForeground(java.awt.Color.RED);
                labelMessage.setFont(new java.awt.Font("Dialog", Font.BOLD, 12));
                add(labelMessage, BorderLayout.CENTER);

                buttonOK = new Button();
                formPanel = new Panel();
                addWindowListener(new WindowAdapter() {
                        @Override
                        public void windowClosing(WindowEvent e) {
                                finish();
                        }
                });

                setSize(dimension);
                setCenterToParent();
        }

        /**
         * Action performed
         * @param e ActionEvent
         */
        public void actionPerformed(ActionEvent e) {
                finish();
        }

        /**
         * Close Dialog Window
         */
        public void finish() {
                responded();
                setVisible(false);
        }

        /**
         * Wait for user response
         */
        public synchronized void waitForResponse() {
                try {
                        while (stillWaiting) {
                                wait();
                        }
                } catch (Exception e) {
                        //ignore
                }
        }

        /**
         *
         */
        public synchronized void responded() {
                stillWaiting = false;
                notifyAll();
        }

        /**
         *
         * @param listener
         */
        public void dialogActionListener(ActionListener listener) {
                buttonOK = new Button("Ok");
                buttonOK.addActionListener(listener);
                formPanel.add(buttonOK, BorderLayout.CENTER);
                add(formPanel, BorderLayout.SOUTH);
        }

        /**
         * This method will set the position to centre of parent window.
         */
        private void setCenterToParent() {
                Frame parent = (Frame) getParent();
                Point parentLocation = parent.getLocation();
                Dimension d = parent.getSize();
                int x = parentLocation.x + ((d.width - getWidth()) / 2);
                int y = parentLocation.y + ((d.height - getHeight()) / 2);
                setLocation(x, y);
        }
}
